package com.xxkj.drugtraceability.ui.drugstorage

import android.content.Intent
import android.text.Html
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.google.gson.Gson
import com.xxkj.drugtraceability.R
import com.xxkj.drugtraceability.bean.DrugInStorageBean
import com.xxkj.drugtraceability.databinding.LayoutDrugItemBinding
import com.xxkj.drugtraceability.ext.clickN
import com.xxkj.drugtraceability.ui.drugscan.DrugScanActivity

/**
 * author: zhoujingjin
 * created on: 2024/9/10 16:52
 * description:
 */
class DrugStorageAdapter(val action:(data:String)->Unit):BaseQuickAdapter<DrugInStorageBean, BaseDataBindingHolder<LayoutDrugItemBinding>>(R.layout.layout_drug_item) {
    var hasData = false
    override fun convert(
        holder: BaseDataBindingHolder<LayoutDrugItemBinding>,
        item: DrugInStorageBean
    ) {
        holder.dataBinding?.apply {
            if (!item.drugtracinfo.isNullOrEmpty()) {
                btnTrace.text = "查看追溯码"
            } else {
                btnTrace.text = "采集追溯码"
            }
            val buffer = StringBuffer()
            buffer.append("<font color='#33333F' size='50px'>")
            buffer.append("<b>"+item.drugName+"    "+"</b>")
            buffer.append("</font>")
            buffer.append("<font color='#494953' size='24px'>")
            buffer.append("${item.drugSpec?:""}/${item.drugUnit}")
            buffer.append("</font>")
            tvContent.text = Html.fromHtml(buffer.toString())

            val buffer1 = StringBuffer()
            buffer1.append("<font color='#33333F'>")
            buffer1.append("${item.cnt}${item.drugUnit}    ")
            buffer1.append("</font>")
            buffer1.append("<font color='#494953'>")
            buffer1.append("已扫/未扫：  ")
            buffer1.append("</font>")
            buffer1.append("<font color='#42C697'>")
            if (item.drugtracinfo.isNullOrEmpty())
            buffer1.append(0)
            else buffer1.append(item.drugtracinfo?.size?:0)
            buffer1.append("</font>")
            buffer1.append("<font color='#494953'>")
            buffer1.append("/")
            buffer1.append("</font>")
            buffer1.append("<font color='#F7745C'>")
            if (item.drugtracinfo.isNullOrEmpty())
            buffer1.append(item.cnt)
            else{
                if (item.cnt<=item.drugtracinfo?.size?:0){
                    buffer1.append(0)
                }else{
                    buffer1.append(item.cnt- item.drugtracinfo?.size!!)
                }
            }
            buffer1.append("</font>")
            tvDetail.text = Html.fromHtml(buffer1.toString())
            btnTrace.clickN {
                action.invoke(Gson().toJson(item))
                //点击去到扫追溯码页面
            }
        }
    }
}